// pages/mylist/myinfor/edit/edit.js
const app = getApp()
const URL = app.globalData.localURL
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userlist: {}, //用户信息列表
        showTopTips: false,
        formData: {},
        openid: '',
        rules: [
            {
                name: 'name',
                rules: {required: true, minlength: 2, message: '姓名最小长度为2'},
            },
            {
                name: 'sex',
                rules: {required: true, minlength: 1, message: '性别最小长度为1'},
            },
            {
                name: 'age',
                rules: {required: true, minlength: 1, message: '年龄最小长度为1'},
            },
            {
                name: 'id_number',
                rules: [{required: true, message: '身份证号必填！'}, {
                    required: true,
                    minlength: 18,
                    maxlength: 18,
                    message: '身份证号位数错误！'
                }],
            },
            {
                name: 'address',
                rules: {required: true, message: '地址必填'},
            },
            {
                name: 'phone_number',
                rules: [{required: true, message: '手机号必填'}, {mobile: true, message: '手机号格式不对'}],
            },
        ]
    },

    //自动获取用户信息
    getUserInfor: function () {
        let that = this;
        wx.request({
            url: URL + '/user_wechat/findUserInfor',
            method: 'POST',
            data: {
                'open_id': that.data.openid,
                'role_id': app.globalData.user.role_id
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (req) {
                if (req.data) {
                    that.setData({
                        userlist: req.data
                    })
                }
            },
            fail: function () {
                console.log("用户信息获取失败")
            }
        })
    },


    //表单提交
    formInputChange(e) {
        const {field} = e.currentTarget.dataset
        this.setData({
            [`userlist.${field}`]: e.detail.value
        })
    },
    submitForm() {
        let that = this;
        let user = app.globalData.user;
    
        this.selectComponent('#form').validate((valid, errors) => {
            if (!valid) {
                const firstError = Object.keys(errors)
                if (firstError.length) {
                    this.setData({
                        error: errors[firstError[0]].message
                    })
                }
            } else {
              wx.request({
                url: URL + '/user/updateUserByOpenId',
                method: "POST",
                data: {
                  'name': that.data.userlist.name,
                  'sex': that.data.userlist.sex,
                  'age': that.data.userlist.age,
                  'phone_number': that.data.userlist.phone_number,
                  'address': that.data.userlist.address,
                  'id_number': that.data.userlist.id_number,
                  'open_id': that.data.openid,
                },
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (data) {
                  console.log(data);
                  if (data.data > -1) {
                    wx.showToast({
                      title: '成功保存'
                    });
                    //信息保存成功自动返回到个人信息页
                    setTimeout(function () {
                      wx.navigateBack({
                        data: 1
                      })
                    }, 1000)
                  }
                }
              })
            }
        })
      },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        //获取openid
        this.setData({
            openid: app.globalData.myopenid
        });
        //加载个人信息
        this.getUserInfor();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
})